/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AssignFields.java
 *
 * Created on 9 Φεβ 2011, 9:55:23 πμ
 */
package importDb.base;

import importDb.base.ImportConstants;
import com.googlecode.svalidators.formcomponents.ValidationGroup;
import com.googlecode.svalidators.validators.FilepathValidator;
import com.googlecode.svalidators.validators.RequiredValidator;
import components.MyDraggable;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import receipts.Main;
import tools.Helper;
import tools.options.Options;

/**
 *
 * @author ssoldatos
 */
public class AssignFields extends MyDraggable {

  private static final long serialVersionUID = 34535435345L;
  private final ArrayList<String[]> data;
  DefaultComboBoxModel[] model = new DefaultComboBoxModel[5];
  DefaultTableModel tableModel = new DefaultTableModel();
  public boolean assigned = false;
  public boolean headers;
  public int[] fields = new int[5];
  private final File file;
  public String database;
  public int thousandsDel;
  public int decimalDel;
  public int multiDecimalDel;
  private boolean hasComma = false;
  private boolean hasStop = false;

  /** Creates new form AssignFields */
  public AssignFields(File file, ArrayList<String[]> dataList) {
    this.data = dataList;
    this.file = file;
    Main.glassPane.activate(null);
    database = file.getName().substring(0, file.getName().lastIndexOf("."));
    createComboModels(false);
    createTableModel(false);
    initComponents();
    tf_database.addValidator(new FilepathValidator(database, false));
    setLocationRelativeTo(null);
    setVisible(true);


  }

  /** This method is called from within the constructor to
   * initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is
   * always regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    jPanel2 = new javax.swing.JPanel();
    jPanel1 = new javax.swing.JPanel();
    jLabel2 = new javax.swing.JLabel();
    cb_headers = new javax.swing.JCheckBox();
    jLabel3 = new javax.swing.JLabel();
    jLabel4 = new javax.swing.JLabel();
    jLabel5 = new javax.swing.JLabel();
    jLabel6 = new javax.swing.JLabel();
    combo_afm = new javax.swing.JComboBox();
    combo_amount = new javax.swing.JComboBox();
    combo_date = new javax.swing.JComboBox();
    combo_type = new javax.swing.JComboBox();
    jLabel7 = new javax.swing.JLabel();
    combo_multiplier = new javax.swing.JComboBox();
    jScrollPane1 = new javax.swing.JScrollPane();
    table = new javax.swing.JTable();
    jLabel8 = new javax.swing.JLabel();
    tf_database = new com.googlecode.svalidators.formcomponents.STextField(new RequiredValidator());
    jLabel9 = new javax.swing.JLabel();
    combo_thousand = new javax.swing.JComboBox();
    jLabel10 = new javax.swing.JLabel();
    combo_decimal = new javax.swing.JComboBox();
    jLabel11 = new javax.swing.JLabel();
    combo_mult_decimal = new javax.swing.JComboBox();
    jLabel1 = new javax.swing.JLabel();
    bt_cancel = new javax.swing.JButton();
    bt_ok = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);

    jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

    jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

    jLabel2.setText("Αναγωγή πεδίων :");

    cb_headers.setText("Η πρώτη γραμμή του αρχείου είναι επικεφαλίδες");
    cb_headers.setOpaque(false);
    cb_headers.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        cb_headersActionPerformed(evt);
      }
    });

    jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
    jLabel3.setText("Πεδίο ΑΦΜ :");

    jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
    jLabel4.setText("Πεδίο Ημ/νίας :");

    jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
    jLabel5.setText("Πεδίο Ποσού :");

    jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
    jLabel6.setText("Πεδίο Είδους :");

    combo_afm.setModel(model[ImportConstants.AFM]);

    combo_amount.setModel(model[ImportConstants.AMOUNT]);
    combo_amount.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        combo_amountActionPerformed(evt);
      }
    });

    combo_date.setModel(model[ImportConstants.DATE]);

    combo_type.setModel(model[ImportConstants.TYPE]);

    jLabel7.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
    jLabel7.setText("Πεδίο πολλ/στη:");

    combo_multiplier.setModel(model[ImportConstants.MULTIPLIER]);
    combo_multiplier.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        combo_multiplierActionPerformed(evt);
      }
    });

    table.setModel(tableModel);
    jScrollPane1.setViewportView(table);

    jLabel8.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
    jLabel8.setText("Όνομα Βάσης :");

    tf_database.setText(database);
    tf_database.setName("Όνομα Βάσης"); // NOI18N

    jLabel9.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
    jLabel9.setText("Διαχ. χιλιάδας :");

    combo_thousand.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Κανένας", ",", "." }));

    jLabel10.setText("Διαχ. δεκαδικών :");

    combo_decimal.setModel(new javax.swing.DefaultComboBoxModel(new String[] { ".", "," }));

    jLabel11.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
    jLabel11.setText("Διαχ. δεκαδικών  ή ποσοστό %:");

    combo_mult_decimal.setModel(new javax.swing.DefaultComboBoxModel(new String[] { ",", ".", "%" }));

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(jPanel1Layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addGroup(jPanel1Layout.createSequentialGroup()
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                  .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                  .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                  .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                  .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                  .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                  .addComponent(combo_afm, javax.swing.GroupLayout.PREFERRED_SIZE, 165, javax.swing.GroupLayout.PREFERRED_SIZE)
                  .addComponent(combo_amount, javax.swing.GroupLayout.PREFERRED_SIZE, 165, javax.swing.GroupLayout.PREFERRED_SIZE)
                  .addComponent(combo_date, javax.swing.GroupLayout.PREFERRED_SIZE, 165, javax.swing.GroupLayout.PREFERRED_SIZE)
                  .addComponent(combo_type, 0, 165, Short.MAX_VALUE)
                  .addComponent(combo_multiplier, 0, 165, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                  .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                      .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                      .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                      .addComponent(tf_database, javax.swing.GroupLayout.PREFERRED_SIZE, 225, javax.swing.GroupLayout.PREFERRED_SIZE)
                      .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(combo_thousand, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel10)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(combo_decimal, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                  .addGroup(jPanel1Layout.createSequentialGroup()
                    .addComponent(jLabel11, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(combo_mult_decimal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
              .addComponent(cb_headers)
              .addComponent(jLabel2))
            .addGap(209, 209, 209))
          .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 663, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(150, 150, 150))))
    );

    jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jLabel3, jLabel4, jLabel5, jLabel6, jLabel7});

    jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {combo_afm, combo_amount, combo_date, combo_multiplier, combo_type});

    jPanel1Layout.setVerticalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(jPanel1Layout.createSequentialGroup()
        .addContainerGap()
        .addComponent(jLabel2)
        .addGap(9, 9, 9)
        .addComponent(cb_headers)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
          .addComponent(jLabel3)
          .addComponent(combo_afm, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(jLabel8)
          .addComponent(tf_database, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(combo_amount, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(jLabel5)
          .addComponent(combo_decimal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(jLabel9)
          .addComponent(combo_thousand, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(jLabel10))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(combo_date, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(jLabel4))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(combo_type, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(jLabel6))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(jLabel7)
          .addComponent(combo_multiplier, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(jLabel11)
          .addComponent(combo_mult_decimal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addGap(45, 45, 45)
        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 220, Short.MAX_VALUE)
        .addContainerGap())
    );

    jLabel1.setFont(jLabel1.getFont().deriveFont(jLabel1.getFont().getStyle() | java.awt.Font.BOLD, jLabel1.getFont().getSize()+2));
    jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    jLabel1.setText("Δημιουργία Βάσης από αρχείο");

    bt_cancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/exit.png"))); // NOI18N
    bt_cancel.setToolTipText("Κλείσιμο");
    bt_cancel.setBorder(null);
    bt_cancel.setBorderPainted(false);
    bt_cancel.setContentAreaFilled(false);
    bt_cancel.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
    bt_cancel.setFocusPainted(false);
    bt_cancel.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        bt_cancelActionPerformed(evt);
      }
    });

    bt_ok.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/ok.png"))); // NOI18N
    bt_ok.setToolTipText("OK");
    bt_ok.setBorder(null);
    bt_ok.setBorderPainted(false);
    bt_ok.setContentAreaFilled(false);
    bt_ok.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
    bt_ok.setFocusPainted(false);
    bt_ok.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        bt_okActionPerformed(evt);
      }
    });

    javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
    jPanel2.setLayout(jPanel2Layout);
    jPanel2Layout.setHorizontalGroup(
      jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(jPanel2Layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addGroup(jPanel2Layout.createSequentialGroup()
            .addComponent(jPanel1, 0, 708, Short.MAX_VALUE)
            .addGap(10, 10, 10))
          .addGroup(jPanel2Layout.createSequentialGroup()
            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 682, Short.MAX_VALUE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(bt_cancel, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
          .addComponent(bt_ok, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)))
    );
    jPanel2Layout.setVerticalGroup(
      jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(jPanel2Layout.createSequentialGroup()
        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addComponent(bt_cancel, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addGroup(jPanel2Layout.createSequentialGroup()
            .addGap(11, 11, 11)
            .addComponent(jLabel1)))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(bt_ok, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );

    pack();
  }// </editor-fold>//GEN-END:initComponents

  private void bt_cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_cancelActionPerformed
    assigned = false;
    Main.glassPane.deactivate();
    dispose();
  }//GEN-LAST:event_bt_cancelActionPerformed

  private void cb_headersActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cb_headersActionPerformed
    createComboModels(cb_headers.isSelected());
    createTableModel(cb_headers.isSelected());
    applyModels();
  }//GEN-LAST:event_cb_headersActionPerformed

  private void bt_okActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_okActionPerformed
    ValidationGroup group = new ValidationGroup();
    group.addComponent(tf_database);
    if (group.validate()) {
      headers = cb_headers.isSelected();
      fields[ImportConstants.AFM] = combo_afm.getSelectedIndex() - 1;
      fields[ImportConstants.AMOUNT] = combo_amount.getSelectedIndex() - 1;
      fields[ImportConstants.DATE] = combo_date.getSelectedIndex() - 1;
      fields[ImportConstants.TYPE] = combo_type.getSelectedIndex() - 1;
      fields[ImportConstants.MULTIPLIER] = combo_multiplier.getSelectedIndex() - 1;
      assigned = true;
      database = tf_database.getText().trim();
      thousandsDel = combo_thousand.getSelectedIndex();
      decimalDel = combo_decimal.getSelectedIndex();
      multiDecimalDel = combo_mult_decimal.getSelectedIndex();
      File db = new File(Options.USER_DIR+Options.DB_PATH+database+".db");
      if (fields[ImportConstants.AFM] == ImportConstants.NOT_EXIST
          || fields[ImportConstants.AMOUNT] == ImportConstants.NOT_EXIST
          || fields[ImportConstants.DATE] == ImportConstants.NOT_EXIST) {
        Helper.message("Τα πεδία ΑΦΜ, ΠΟΣΟ, ΗΜΕΡΟΜΗΝΙΑ πρέπει να υπάρχουν", "Δημιουργία βάσης από αρχείο", JOptionPane.ERROR_MESSAGE);
      } else if(db.exists()){
        Helper.message("Η βάση με το όνομα " + database +" υπάρχει ήδη", "Δημιουργία βάσης από αρχείο", JOptionPane.ERROR_MESSAGE);
      } else {
        Main.glassPane.deactivate();
        dispose();
      }
    } else {
      group.errorMessage(true);
    }
  }//GEN-LAST:event_bt_okActionPerformed

  private void combo_amountActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_combo_amountActionPerformed
    String amount = getGreatestAmount(combo_amount.getSelectedIndex() - 1);

    int posComma = amount.indexOf(",");
    int posStop = amount.indexOf(".");
    if (posComma > -1 && posStop > -1) {
      if (posComma < posStop) {
        setThousandsDel(',');
        setDecimalDel('.');
      } else {
        setThousandsDel('.');
        setDecimalDel(',');
      }
    } else if (posComma > -1) {
      if (posComma >= (amount.length() - 3)) {
        setDecimalDel(',');
        if (hasStop) {
          setThousandsDel('.');
        }
      } else {
        setThousandsDel(',');
        if (hasComma) {
          setDecimalDel('.');
        }
      }
    } else if (posStop > -1) {
      if (posStop >= (amount.length() - 3)) {
        setDecimalDel('.');
        if (hasComma) {
          setThousandsDel(',');
        }
      } else {
        setThousandsDel('.');
        if (hasStop) {
          setDecimalDel(',');
        }
      }
    }
  }//GEN-LAST:event_combo_amountActionPerformed

  private void combo_multiplierActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_combo_multiplierActionPerformed
    String mult = getGreatestAmount(combo_multiplier.getSelectedIndex() - 1);
    int posComma = mult.indexOf(",");
    int posStop = mult.indexOf(".");
    if(posComma > -1){
      setMultDecimalDel(',');
    } else if(posStop > -1){
      setMultDecimalDel('.');
    } else {
      if(mult.length()>=3){
        setMultDecimalDel('%');
      } else {
        setMultDecimalDel('.');
      }
    }
  }//GEN-LAST:event_combo_multiplierActionPerformed

  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JButton bt_cancel;
  private javax.swing.JButton bt_ok;
  private javax.swing.JCheckBox cb_headers;
  private javax.swing.JComboBox combo_afm;
  private javax.swing.JComboBox combo_amount;
  private javax.swing.JComboBox combo_date;
  private javax.swing.JComboBox combo_decimal;
  private javax.swing.JComboBox combo_mult_decimal;
  private javax.swing.JComboBox combo_multiplier;
  private javax.swing.JComboBox combo_thousand;
  private javax.swing.JComboBox combo_type;
  private javax.swing.JLabel jLabel1;
  private javax.swing.JLabel jLabel10;
  private javax.swing.JLabel jLabel11;
  private javax.swing.JLabel jLabel2;
  private javax.swing.JLabel jLabel3;
  private javax.swing.JLabel jLabel4;
  private javax.swing.JLabel jLabel5;
  private javax.swing.JLabel jLabel6;
  private javax.swing.JLabel jLabel7;
  private javax.swing.JLabel jLabel8;
  private javax.swing.JLabel jLabel9;
  private javax.swing.JPanel jPanel1;
  private javax.swing.JPanel jPanel2;
  private javax.swing.JScrollPane jScrollPane1;
  private javax.swing.JTable table;
  private com.googlecode.svalidators.formcomponents.STextField tf_database;
  // End of variables declaration//GEN-END:variables

  private void createComboModels(boolean headers) {
    String[] fakeArr = new String[this.data.get(0).length];
    for (int i = 0; i < this.data.get(0).length; i++) {
     fakeArr[i] = "Πεδίο " + (i+1);
    }
    String[] or = (headers) ? this.data.get(0) : fakeArr;
    String[] tmp = new String[or.length + 1];
    System.arraycopy(or, 0, tmp, 1, or.length);
    tmp[0] = ImportConstants.NONE;
    for (int i = 0; i < 5; i++) {
      model[i] = new DefaultComboBoxModel(tmp);
    }
  }

  private void applyModels() {
    combo_afm.setModel(model[ImportConstants.AFM]);
    combo_amount.setModel(model[ImportConstants.AMOUNT]);
    combo_date.setModel(model[ImportConstants.DATE]);
    combo_type.setModel(model[ImportConstants.TYPE]);
    combo_multiplier.setModel(model[ImportConstants.MULTIPLIER]);
    table.setModel(tableModel);
  }

  private void createTableModel(boolean headers) {
    Vector<Vector<String>> rows = new Vector<Vector<String>>();
    Vector<String> columnNames = new Vector<String>();
    int i = 0;
    for (Iterator<String[]> it = data.iterator(); it.hasNext();) {
      String[] strings = it.next();
      if (i == 0) {
        i++;
        if (headers) {
          columnNames.addAll(Arrays.asList(strings));
        } else {
          for (int j = 0; j < strings.length; j++) {
            columnNames.add("Πεδίο " + (j+1));
          }
          rows.add(new Vector<String>(Arrays.asList(strings)));
        }
      } else {
        rows.add(new Vector<String>(Arrays.asList(strings)));
      }
    }
    tableModel = new DefaultTableModel(rows, columnNames);
  }

  private void setThousandsDel(char c) {
    combo_thousand.setSelectedItem(String.valueOf(c));
  }

  private void setDecimalDel(char c) {
    combo_decimal.setSelectedItem(String.valueOf(c));
  }

  private String getGreatestAmount(int amountField) {
    int l = -1;
    String grAmount = "";
    for (Iterator<String[]> it = data.iterator(); it.hasNext();) {
      String[] strings = it.next();
      if (l > -1 || !cb_headers.isSelected()) {
        String amount = strings[amountField].replaceAll("[^\\d\\.,]", "");
        if (amount.indexOf(",") > -1) {
          hasComma = true;
        }
        if (amount.indexOf(".") > -1) {
          hasStop = true;
        }
        if (amount.length() > l) {
          l = amount.length();
          grAmount = amount;
        }
      } else {
        l=0;
      }

    }
    return grAmount;
  }

  private void setMultDecimalDel(char c) {
    combo_mult_decimal.setSelectedItem(String.valueOf(c));
  }
}
